<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Http\Requests\CategoriesRequest;
use App\Model\Admin\CategoriesModel;
use App\Model\Admin\SystemModel;
use Illuminate\Http\Request;

class CategoryController extends Controller
{
    protected $fields = [
        'pid' => '',
        'name' => '',
        'model_id' => '',
        'order' => '',
        'keywords' => '',
        'description' => '',
    ];

    /**
     * 列表
     *
     * @return \Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function index()
    {
        return view('admin.categories.index');
    }

    /**
     * Show the form for creating a new resource.
     *
     */
    public function create()
    {
        // 上级分类
        $data['parent'] = $this->listTree();
        // 关联模型
        $data['model'] = SystemModel::get();

        return view('admin.categories.add')->with('data',$data);
    }

    /**
     * 保存
     *
     * @param CategoriesRequest $request
     * @return array
     */
    public function store(CategoriesRequest $request)
    {
        $Cate = new CategoriesModel();
        foreach ($this->fields as $k => $v){
            $Cate -> $k = $request->get($k, $v);
        }

        if ($Cate -> save()){
            return $this->res_msg(0, '创建成功');
        } else{
            return $this->res_msg(1, '创建失败');
        }
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     */
    public function edit($id)
    {
        // 上级分类
        $data['parent'] = $this->listTree();
        // 关联模型
        $data['model'] = SystemModel::get();

        $data['info'] = ['id' => $id];

        $cate = CategoriesModel::findOrFail($id);

        foreach ($this->fields as $k => $v){
            $data['info'][$k] = $cate->$k;
        }
        return view('admin.categories.edit')->with('data',$data);
    }

    /**
     * 保存修改
     *
     * @param CategoriesRequest $request
     * @param $id
     * @return array
     */
    public function update(CategoriesRequest $request, $id)
    {
        $cate = CategoriesModel::findOrFail($id);

        foreach ($this->fields as $k => $v){
            $cate -> $k = $request->get($k, $v);
        }

        if ($cate->save()){
            return $this->res_msg(0, '修改成功');
        } else {
            return $this->res_msg(1, '修改失败');
        }

    }

    /**
     * 删除
     *
     * @param $id
     * @return array
     * @throws \Exception
     */
    public function destroy($id)
    {
        $cate = CategoriesModel::findOrFail($id);
        if ($cate->delete()){
            return $this->res_msg(0,'成功');
        } else{
            return $this->res_msg(1,'删除失败');
        }
    }

    /**
     * 列表数据
     *
     * @return \Illuminate\Http\JsonResponse
     */
    public function list()
    {
        $data['code'] = 0;
        $data['msg'] = '成功';

        $Cate = new CategoriesModel();
        $data['count'] = $Cate->count();

        $data['data'] = $Cate
            ->leftjoin('model','model.id','=','categories.model_id')
            ->select('model.name as model_name','categories.*')
            ->orderBy('order')
            ->get()->toArray();

        return response()->json($data);
    }

    /**
     * 获取树形列表数据
     *
     * @return array
     */
    public function listTree()
    {
        $Cate = new CategoriesModel();
        $categories = $Cate
            ->orderBy('order')
            ->get()->toArray();
        $categories_tree = $this->buildMenuChild(0,$categories);
        return $categories_tree;
    }

    /**
     *  生成菜单树
     *
     * @param $pid
     * @param $menu_list
     * @return array
     */
    private function buildMenuChild($pid, $menu_list)
    {
        $tree_list = [];
        foreach ($menu_list as $v) {
            if ($v['pid'] == $pid) {
                $node = (array)$v;

                $child = $this->buildMenuChild($v['id'], $menu_list);

                if (!empty($child)) {
                    $node['child'] = $child;
                }
                $tree_list[] = $node;
            }
        }
        return $tree_list;
    }
}
